<script src="../main.js"></script>
<template>
    <div>
        <div class="background">
             <img src="../assets/src=http___i0.hdslb.com_bfs_article_acce58eaf2a574b180c8e7c6ce131edccf5fb282.gif&refer=http___i0.hdslb.gif" width="100%" height="100%"/>
        </div>
        <div  class="front">
            <!--登录表单-->
            <div class="login_box">
                   <div style="margin-top: 50px;margin-left: 50px;width: 300px;text-align: center;">
                       <p style="color: black;">密  码  登  陆</p>
                   </div>

                    <el-input
                            style="margin-top: 70px;margin-left: 50px;margin-right: 50px;width: 300px;height: 50px"
                            prefix-icon="el-icon-user-solid"
                            placeholder="用户名"
                            prop="username"
                            v-model="ruleForm.username"
                            clearable>
                    </el-input>

                    <el-input
                            style="margin-top: 20px;margin-left: 50px;margin-right: 50px;width: 300px;height: 50px"
                            prefix-icon="el-icon-lock"
                            placeholder="密码"
                            v-model="ruleForm.password"
                            prop="password"
                            show-password
                            clearable>
                    </el-input>
                    <el-button style="background-color: red;margin-top: 50px;margin-left: 50px;margin-right: 50px;width: 300px" @click="submitForm('ruleForm')">立即登录</el-button>
                        <!--<el-button type="primary" @click="resetForm('ruleForm')">重置</el-button>-->
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data() {
            return {
                ruleForm: {
                    username: '',
                    password: '',
                },
                rules: {
                    username: [
                        {required: true, message: '请输入胡用户名！', trigger: 'blur'},
                        {min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码！', trigger: 'blur'}
                    ],

                }
            };
        },
        methods: {

            /**提交表单**/
            async submitForm(formName) {
                // this.$refs[formName].validate((valid) => {
                //     if (valid) {
                        // alert('submit!');
                        //提交登录信息
                        //获取到当前的this对象
                        const _this = this;
                        this.$axios.post("/login", this.ruleForm).then(res => {

                            console.log(res.data)
                            const jwt = res.headers["authorization"]
                            if (jwt === null){
                                this.$alert('用户名或密码错误！！', '提示', {
                                    confirmButtonText: '确定',
                                    callback: action => {
                                        // _this.$router.push("/blogs")
                                    }
                                });
                            }else {
                                const userInfo = res.data.data
                                console.log(jwt)
                                console.log(userInfo)

                                //把数据共享出去
                                _this.$store.commit("SET_TOKEN", jwt);
                                _this.$store.commit("SET_USERINFO", userInfo);

                                //获取
                                console.log(_this.$store.getters.getUser)
                                //页面跳转
                                _this.$router.push("/main")
                            }


                        });
                    // } else {
                    //     console.log('error submit!!');
                    //     return false;
                    // }

            // resetForm(formName) {
            //     this.$refs[formName].resetFields();
            // }
        }
    },
    }

</script>

<style scoped>

    .login_box {
        height: 350px;
        width: 400px;
        margin-left: 40%;
        margin-top: 15%;
        background-color: darkgrey;
        opacity: 0.5;
    }

    .el-aside {
        background-color: #D3DCE6;
        color: #333;
        text-align: center;
        line-height: 200px;
    }

    .el-main {
        /*background-color: #E9EEF3;*/
        color: #333;
        text-align: center;
        line-height: 160px;
        margin-top: 300px;
    }

    body > .el-container {
        margin-bottom: 40px;
    }

    .el-container:nth-child(5) .el-aside,
    .el-container:nth-child(6) .el-aside {
        line-height: 260px;
    }

    .el-container:nth-child(7) .el-aside {
        line-height: 320px;
    }

    .mlogo {
        height: 100px;
    }

    .demo-ruleForm {
        max-width: 400px;
        margin: 0 auto;
    }

    .background{
        width:100%;
        height:100%;  /**宽高100%是为了图片铺满屏幕 */
        z-index:-1;
        position: absolute;
    }

    .front{
        z-index:1;
        width:100%;
        height:100%;
        position: absolute;
    }

</style>

